package mop.domain.user;

import java.util.LinkedList;
import java.util.List;

import mop.exception.*;

/**
 * Class for UserPool
 * this class is a singleton !
 * 
 * @invar 	no two users have the same name
 * 			|foreach(name) 
 *  		| isUniqueUser(name)
 */
public class UserPool 
{

	/**
	 * Create a UserPool with no extra settings.
	 * Constructor must be at protected level.
	 * Allows inheritance but no out of package creation.
	 * 
	 * @post users is set to a new LinkedList
	 */
	protected UserPool()
	{
		this.users = new LinkedList<User>();
	}
	
	
	/***************************************************************************
	 * 							instance
	 ***************************************************************************/
	
	/**
	 * Use this function to acces UserPool always
	 * 
	 * @return the static instance of UserPool
	 */
	public static UserPool getInstance()
	{
		if(instance == null) 
		{
	         instance = new UserPool();
	    }
		return instance;	
	}
	
	/**
	 * instance to self
	 */
	private static UserPool instance = null;
	
	
	/***************************************************************************
	 * 							Users container
	 ***************************************************************************/
	
	/**
	 * return a list of all the users in users
	 * 
	 * @return a the list of users by string (in this case their name) !
	 */
	public List<String> getUserList()
	{
		List<String> result = new LinkedList<String>();
		
		for(User p : users)
		{
			result.add(p.getName());
		}
		
		return result;
	}
		
	 /**
	  * checks if the passed name is not already a user 
	  * in the container 
	  * Use this function to check wether a user exists
	  * in login and createion
	  * 
	  * @param name
	  *  the name you want checked
	  * 
	  * @return true if name cannot be found in the database
	  */
	 public boolean hasUser(String name)
	 {
		 for(User p: users)
		 {
			 if(p.getName().equals(name))
				 return true;
		 }
		 return false;
	 }
	
	/**
	 * add a user to the container
	 * 
	 * @param user
	 * 	the name of the user you want to create
	 * 
	 * @post a new user instance is created with name and description
	 * 
	 * @throws IllegalDuplicateException
	 * 		if the user is a duplicate
	 * 		|!isUniqueUser(name)
	 * 
	 * from the class user it throws  :
	 * @throws IllegalNameException 
	 *       if the name isn't a non empty string
     *       |!isValidName(name) 
	 *       
	 */
	public void addUser(String user) throws IllegalNameException,IllegalDuplicationException
	{
		if(!hasUser(user))
		{
			User p = new User(user);
			this.users.add(p);
		}
		else
		{
			throw  new IllegalDuplicationException(user);
		}
	}
	 
	/**
	 * list to store the users in 
	 */
	private List<User> users;

	
	/***************************************************************************
	 * 							Current user
	 ***************************************************************************/
	
	/**
	 * get the current user name
	 * 
	 * @return currentuser name , returns null if none has yet been set
	 */
	public String getCurrentUser()
	{
		return currentUser.getName();
	}
	
	/**
	 * set the current user
	 * 
	 * @param name 
	 *  the name of the user that should be currentUser
	 * 
	 * @post the current user variable is set to the user with that name
	 * 
	 * @throws IllegalUserException
	 * 		if the user already exists throw exception
	 * 		| hasUser(name)
	 */
	public void setCurrentUser(String name) throws IllegalUserException
	{
		for(User p: users)
		 {
			 if(p.getName().equals(name))
			 {
				 currentUser = p;
				 return;
			 }
		 }
		
		throw new IllegalUserException(name);
	}
	
	/**
	 * the current user of the system
	 */
	private User currentUser = null;
}
